/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package apus.action;

import apus.authorisation.UserAuthorisation;
import apus.persistence.UserDAO;
import apus.services.ServiceLocator;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author 21vek
 */
public class PasswordSaveAction implements Action {

    @Override
    public String getName() {
        return "password.save";
    }

    @Override
    public ActionResult execute(HttpServletRequest request, HttpServletResponse response) {
        UserDAO userDAO = ServiceLocator.getService(UserDAO.class);
        UserAuthorisation ua = (UserAuthorisation) request.getSession().getAttribute("userAuth");
        if (userDAO.checkAuthorisation(ua.getLogin(), request.getParameter("oldPass")) != -1) {
            request.getSession().setAttribute("win", "Password was successfully changed");
            userDAO.changePassword(ua.getLogin(), request.getParameter("newPass"));
        } else {
            request.getSession().setAttribute("fail", "Wrong password");
        }
        return ActionResult.forward("/WEB-INF/View/changePassword.jsp");
       // return ActionResult.forward(request.getHeader("Referer"));
    }
}
